from app import db
from datetime import datetime

class Resource(db.Model):
    __tablename__ = 'resources'
    
    id = db.Column(db.Integer, primary_key=True)
    project_id = db.Column(db.Integer, db.ForeignKey('projects.id'), unique=True, nullable=False)
    budget = db.Column(db.Float)  # 预算
    human_resources = db.Column(db.JSON)  # 人力资源配置
    equipment = db.Column(db.JSON)  # 设备资源配置
    created_at = db.Column(db.DateTime, default=datetime.utcnow)
    updated_at = db.Column(db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
    
    def __init__(self, project_id, budget=None, human_resources=None, equipment=None):
        self.project_id = project_id
        self.budget = budget
        self.human_resources = human_resources or {
            'engineers': 0,
            'technicians': 0
        }
        self.equipment = equipment or {
            'sensors': {'count': 0, 'unit': '个'},
            'controllers': {'count': 0, 'unit': '台'},
            'actuators': {'count': 0, 'unit': '个'}
        }
    
    def __repr__(self):
        return f'<Resource {self.project_id}>'
    
    def to_dict(self):
        return {
            'id': self.id,
            'project_id': self.project_id,
            'budget': self.budget,
            'human_resources': self.human_resources,
            'equipment': self.equipment,
            'created_at': self.created_at.isoformat() if self.created_at else None,
            'updated_at': self.updated_at.isoformat() if self.updated_at else None
        } 